<template>
  <div class="login-page">
    <el-card class="el-card">
      <div class="img">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt="" />
      </div>
      <el-form class="el-form" :model="ruleForm" :rules="rules" ref="form">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user-solid"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="ruleForm.password"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="el-end">
          <el-button type="primary" @click="submit">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { submitApi } from "@/api/user";
import { Message, message } from "element-ui";
import { setToken } from "@/utils/storage";
export default {
  name: "login-page",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    document.querySelector("body").style.backgroundColor = "#2b4b6b";
  },
  beforeDestroy() {
    document.querySelector("body").removeAttribute("style");
  },
  methods: {
    async submit() {
      await this.$refs.form.validate();
      try {
        const res = await submitApi(this.ruleForm);
        console.log(res);
        const token = res.data.token;
        //获取到token存储到本地
        setToken(token);
        message.success("恭喜，登录成功");
        this.$router.push("/");
      } catch (error) {
        Message.error("用户名或密码错误");
      }
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-card {
    position: relative;
    width: 450px;
    min-height: 200px;
    background-color: #fff;
    border-radius: 5px;
    overflow: visible;
    .img {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      transform: translate(90%, -50%);
      width: 150px;
      height: 150px;
      background-color: #fff;
      border-radius: 150px;
      box-shadow: 0 0 3px 3px rgba(245, 244, 244, 0.4);
      img {
        width: 135px;
        height: 135px;
        border-radius: 135px;
        background-color: #eeeeee;
      }
    }
    .el-form {
      margin-top: 100px;
    }
    .el-end {
      display: flex;
      justify-content: flex-end;
      margin-top: 30px;
    }
  }
}
</style>
